package com.nanshenycd.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Object> redisTempalte = new RedisTemplate<>();
        redisTempalte.setKeySerializer(new StringRedisSerializer());
        redisTempalte.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //redis默认的是jdk的序列化=》二进制 产生的数据比较长
        redisTempalte.setHashKeySerializer(new StringRedisSerializer());
        redisTempalte.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        //注入连接工厂
        redisTempalte.setConnectionFactory(redisConnectionFactory);
        return redisTempalte;
    }
}
